package ac.zlf.core.util.security;

import ac.zlf.core.util.encode.Encodes;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.AlgorithmParameterSpec;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class AESEncrypts {
    private static final String ALGORITHM = "AES";
    private static final String ENCODING = "UTF-8";
    private static final String TRANSFORMATION_NAME = "AES/CBC/PKCS5Padding";
    public static byte[] iv = {82, 22, 50, 44, -16, 124, -40, -114, -87, -40, 37, 23, -56, 23, -33, 75};

    public static String encrypt(String content, String password) {
        try {
            if (password == null) {
                System.out.print("key is null");
                return null;
            }
            int mod = password.length() % 16;
            if (mod != 0) {
                for (int i = mod; i < 16; i++) {
                    password = password + "0";
                }
            }
            if (password.length() > 16) {
                password = password.substring(password.length() - 16, password.length());
            }
            AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);
            byte[] enCodeFormat = password.getBytes("UTF-8");
            SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(1, key, paramSpec);
            return Encodes.encodeBase64(cipher.doFinal(content.getBytes("UTF-8")));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String decrypt(String content, String password) {
        try {
            if (password == null) {
                System.out.print("key is null");
                return null;
            }
            int mod = password.length() % 16;
            if (mod != 0) {
                for (int i = mod; i < 16; i++) {
                    password = password + "0";
                }
            }
            if (password.length() > 16) {
                password = password.substring(password.length() - 16, password.length());
            }
            AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);
            byte[] enCodeFormat = password.getBytes("UTF-8");
            SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(2, key, paramSpec);
            return new String(cipher.doFinal(Encodes.decodeBase64(content)));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) throws Exception {
        long x = System.currentTimeMillis();
        String content = "+ELwgUA2DPoiRR2H9jxT/bmrpFo=|51c2af03e4b072e491bbb6ae|" + x;
        String password = "hansen988";


        content = encrypt(content, password);
        System.out.println(content);

        System.out.println(decrypt(content, password));
    }
}